package com.kzd.gtgame.main.game


import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.createViewTextBottom
import com.kzd.gtgame.custom.createViewTextMove
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.entity.GameService1
import com.kzd.gtgame.jetpack.vm.GameViewModel
import com.kzd.gtgame.main.game.adapter.GameServiceAdapter
import com.kzd.gtgame.main.toServiceMoreActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.include_app_recycler.*

class GameServiceFragment : OwnerViewModeFragment<GameViewModel>() {

    companion object {
        fun newInstance(): GameServiceFragment {
            return GameServiceFragment()
        }
    }

    private val mGameServiceAdapter = GameServiceAdapter()

    override fun viewModel(): GameViewModel {
        return createViewModel(requireActivity())
    }


    override val layoutResId: Int
        get() = R.layout.include_app_recycler

    override fun initView() {
        rvAppRecyclerBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
            itemAnimator = null
        }.adapter = mGameServiceAdapter

        mGameServiceAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mGameServiceAdapter.getItem(position)
            if (view.id == R.id.tvGameServiceRemindStatus) {
                viewModel.gameServiceRemind(itemData.id, itemData.remind_rs, position)
            }
        }
    }

    override fun initData() {
        viewModel.gameService()
    }

    override fun onRefreshLoad() {
        viewModel.gameService()
    }

    override fun initViewMode() {
        viewModel.gameServiceLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,mGameServiceAdapter)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = (response.data?.data ?: mutableListOf()).apply {
                add(GameService1())
            }
            mGameServiceAdapter.setHeaderView(mHeaderView)
            mGameServiceAdapter.setNewInstance(data)
            mGameServiceAdapter.removeAllFooterView()
            if (data.size >= 10) {
                mGameServiceAdapter.addFooterView(mFooterViewMore)
            }
            mGameServiceAdapter.addFooterView(mFooterViewBottom)
        })

        viewModel.serviceRemindLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data
                ?: return@observe
            val gameService = mGameServiceAdapter.data[data.position].apply {
                this.remind_rs = data.isRemind
            }
            mGameServiceAdapter.setData(data.position, gameService)
        })
    }

    private val mHeaderView: View by lazy {
        createViewTextMove(
            context = requireContext(),
            text = "温馨提示：仅供参考，以以游戏内开服为准",
            textSize = 11f,
            textColor = R.color.FF_999999
        )
    }

    private val mFooterViewMore: View by lazy {
        createViewTextMove(
            context = requireContext(),
            text = "点击查看更多开服>",
            textColor = R.color.colorMain
        ).apply {
            setOnClickListener {
                val gameId = viewModel.gameDetailsLiveData.value
                    ?.data?.data?.id ?: return@setOnClickListener
                val gameName = viewModel.gameDetailsLiveData.value
                    ?.data?.data?.name ?: return@setOnClickListener
                toServiceMoreActivity(gameName, gameId)
            }
        }
    }

    private val mFooterViewBottom: View by lazy {
        createViewTextBottom(context = requireActivity())
    }
}